/**
  ******************************************************************************
  * @file    
  * @author  
  * @version 
  * @date    
  * @brief   
  ******************************************************************************
	* @attention
  
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

#include <stdio.h>
#include <string.h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define bool _Bool
#define FALSE 0
#define TRUE !FALSE

#define SCROLL_SPEED  1000 /* Low value gives higher speed */
#define SCROLL_NUM    20

#define ADC_CONV_BUFF_SIZE 16
#define MAX_CHNL 16


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;

char strDisp[20];
uint32_t ADC_Result, AVG;

volatile _Bool flag_ADCDMA_TransferComplete;
volatile _Bool flag_UserButton;

uint16_t ADC_ConvertedValueBuff[ADC_CONV_BUFF_SIZE];




/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  LAB2 MCU initialization.
  * @param  None
  * @retval None
  */
void LAB1_GetReady(void)
{
	/* Enable clocks for PWR */
	*(__IO uint32_t *) RCC_APB1ENR_PWREN_BB = (uint32_t)SET;
	/* Disable clocks for USART */
	//*(__IO uint32_t *) RCC_APB1ENR_USART2EN_BB = (uint32_t)RESET;
	/* Disable clocks for SYSCFG */
	//*(__IO uint32_t *) RCC_APB2ENR_SYSCFGEN_BB = (uint32_t)RESET;
	/* Disable clocks for GPIOD */
	//*(__IO uint32_t *) RCC_AHBENR_GPIODEN_BB = (uint32_t)RESET;
		
  /* Enter STOP mode as deepsleep */
	*(__IO uint32_t *) PWR_CR_PDDS_BB = (uint32_t)RESET;
	
	/* Put internal voltage regulator in Low-Power mode during the STOP mode */
	*(__IO uint32_t *) PWR_CR_LPSDSR_BB = (uint32_t)SET;
 
  /* Set SLEEPDEEP bit of Cortex System Control Register */
  SCB->SCR |= SCB_SCR_SLEEPDEEP;
	
	/* Set the Wake-Up time in ms */
	RTC_SetWakeUp(40);
	
	/* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;
		
	/* Disable the EXTI event on RTC Wake-Up */
  EXTI->EMR &= ~EXTI_Line20;
	/* Enable the EXTI interrupt on RTC Wake-Up */
  EXTI->IMR |= EXTI_Line20;
		
	/* Clear the RTC wake-up flag */
	*(__IO uint32_t *) RTC_ISR_WUTF_BB = (uint32_t)RESET;
	/* Enable the Wakeup Timer */
	*(__IO uint32_t *) RTC_CR_WUTE_BB = (uint32_t)SET;
}

/**
  * @brief  LAB2 MCU deinitialization.
  * @param  None
  * @retval None
  */
void LAB1_Recover(void)
{
	/* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF;
		
	/* Disable the EXTI event on RTC Wake-Up */
  EXTI->EMR &= ~EXTI_Line20;
		
	/* Disable the EXTI interrupt on RTC Wake-Up */
  EXTI->IMR &= ~EXTI_Line20;
	
	/* Enable clocks for GPIOD */
	*(__IO uint32_t *) RCC_AHBENR_GPIODEN_BB = (uint32_t)SET;
	/* Disable clocks for PWR */
	*(__IO uint32_t *) RCC_APB1ENR_PWREN_BB = (uint32_t)RESET;
	/* Enable clocks for USART2 */
	*(__IO uint32_t *) RCC_APB1ENR_USART2EN_BB = (uint32_t)SET;
	/* Enable clocks for SYSCFG */
	*(__IO uint32_t *) RCC_APB2ENR_SYSCFGEN_BB = (uint32_t)SET;
}


/**
  * @brief  LAB2 MCU initialization.
  * @param  None
  * @retval None
  */
void LAB2_GetReady(void)
{
	/* Enable clocks for PWR */
	*(__IO uint32_t *) RCC_APB1ENR_PWREN_BB = (uint32_t)SET;
	/* Disable clocks for USART */
	//*(__IO uint32_t *) RCC_APB1ENR_USART2EN_BB = (uint32_t)RESET;
	/* Disable clocks for SYSCFG */
	//*(__IO uint32_t *) RCC_APB2ENR_SYSCFGEN_BB = (uint32_t)RESET;
	/* Disable clocks for GPIOD */
	//*(__IO uint32_t *) RCC_AHBENR_GPIODEN_BB = (uint32_t)RESET;
		
  /* Enter STOP mode as deepsleep */
	*(__IO uint32_t *) PWR_CR_PDDS_BB = (uint32_t)RESET;
	
	/* Put internal voltage regulator in Low-Power mode during the STOP mode */
	*(__IO uint32_t *) PWR_CR_LPSDSR_BB = (uint32_t)SET;
 
  /* Set SLEEPDEEP bit of Cortex System Control Register */
  SCB->SCR |= SCB_SCR_SLEEPDEEP;
	
	/* Set the Wake-Up time in ms */
	RTC_SetWakeUp(40);
	
	/* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;
		
	/* Disable the EXTI event on RTC Wake-Up */
  EXTI->EMR &= ~EXTI_Line20;
	/* Enable the EXTI interrupt on RTC Wake-Up */
  EXTI->IMR |= EXTI_Line20;
		
	/* Clear the RTC wake-up flag */
	*(__IO uint32_t *) RTC_ISR_WUTF_BB = (uint32_t)RESET;
	/* Enable the Wakeup Timer */
	*(__IO uint32_t *) RTC_CR_WUTE_BB = (uint32_t)SET;
}

/**
  * @brief  LAB2 MCU deinitialization.
  * @param  None
  * @retval None
  */
void LAB2_Recover(void)
{	
	/* Enable the Wakeup Timer */
	*(__IO uint32_t *) RTC_CR_WUTE_BB = (uint32_t)SET;
	
	/* Disable the EXTI event on RTC Wake-Up */
  EXTI->EMR &= ~EXTI_Line20;
		
	/* Disable the EXTI interrupt on RTC Wake-Up */
  EXTI->IMR &= ~EXTI_Line20;
	
	/* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF;
		
	/* Enable clocks for GPIOD */
	*(__IO uint32_t *) RCC_AHBENR_GPIODEN_BB = (uint32_t)SET;
	/* Disable clocks for PWR */
	*(__IO uint32_t *) RCC_APB1ENR_PWREN_BB = (uint32_t)RESET;
	/* Enable clocks for USART2 */
	*(__IO uint32_t *) RCC_APB1ENR_USART2EN_BB = (uint32_t)SET;
	/* Enable clocks for SYSCFG */
	*(__IO uint32_t *) RCC_APB2ENR_SYSCFGEN_BB = (uint32_t)SET;
}


/**
  * @brief  LAB2 MCU initialization.
  * @param  None
  * @retval None
  */
void LAB3_GetReady(void)
{
	/* Enable clocks for PWR */
	*(__IO uint32_t *) RCC_APB1ENR_PWREN_BB = (uint32_t)SET;
	/* Disable clocks for USART */
	//*(__IO uint32_t *) RCC_APB1ENR_USART2EN_BB = (uint32_t)RESET;
	/* Disable clocks for SYSCFG */
	//*(__IO uint32_t *) RCC_APB2ENR_SYSCFGEN_BB = (uint32_t)RESET;
	/* Disable clocks for GPIOD */
	//*(__IO uint32_t *) RCC_AHBENR_GPIODEN_BB = (uint32_t)RESET;
		
  /* Enter STOP mode as deepsleep */
	*(__IO uint32_t *) PWR_CR_PDDS_BB = (uint32_t)RESET;
	
	/* Put internal voltage regulator in Low-Power mode during the STOP mode */
	*(__IO uint32_t *) PWR_CR_LPSDSR_BB = (uint32_t)SET;
 
  /* Set SLEEPDEEP bit of Cortex System Control Register */
  SCB->SCR |= SCB_SCR_SLEEPDEEP;
	
	/* Disable the EXTI event on RTC Wake-Up */
  EXTI->EMR &= ~EXTI_Line20;
	/* Enable the EXTI interrupt on RTC Wake-Up */
  EXTI->IMR |= EXTI_Line20;
}

/* functions ---------------------------------------------------------*/
	
void insertionSort(uint16_t *numbers, uint32_t array_size) 
{
  
	uint32_t i, j;
	uint32_t index;

  for (i=1; i < array_size; i++) {
    index = numbers[i];
    j = i;
    while ((j > 0) && (numbers[j-1] > index)) {
      numbers[j] = numbers[j-1];
      j = j - 1;
    }
    numbers[j] = index;
  }
}

uint32_t interquartileMean(uint16_t *array, uint32_t numOfSamples)
{
    uint32_t sum=0;
    uint32_t  index, maxindex;
    /* discard  the lowest and the highest data samples */ 
	maxindex = 3 * numOfSamples / 4;
    for (index = (numOfSamples / 4); index < maxindex; index++){
            sum += array[index];
    }
	/* return the mean value of the remaining samples value*/
    return ( sum / (numOfSamples / 2) );
}


void configureWakeup (void)
{

	RTC_Config();
	RTC_SetWakeUp(30);
	
	/* Disable the EXTI event on RTC Wake-Up */
  EXTI->EMR &= ~EXTI_Line20;
		
	/* Enable the EXTI interrupt on RTC Wake-Up */
  EXTI->IMR |= EXTI_Line20;
	
	/* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;
	
		/* Clear the RTC wake-up flag */
	*(__IO uint32_t *) RTC_ISR_WUTF_BB = (uint32_t)RESET;
			
	/* Enable the Wakeup Timer */
	*(__IO uint32_t *) RTC_CR_WUTE_BB = (uint32_t)SET;
	
	
	//   /* Declare initialisation structures for (NVIC) and external interupt (EXTI) */
//   NVIC_InitTypeDef NVIC_InitStructure;
//   EXTI_InitTypeDef EXTI_InitStructure;

//   /* Clear IT pending bit from external interrupt Line 20 */
//   EXTI_ClearITPendingBit(EXTI_Line20);
//   
//   /* Initialise EXTI using its init structure */
//   EXTI_InitStructure.EXTI_Line = EXTI_Line20;			 // interrupt generated on RTC Wakeup event (Line 20)
//   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    // Use EXTI line as interrupt
//   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; // Trigg interrupt on rising edge detection
//   EXTI_InitStructure.EXTI_LineCmd = ENABLE;				 // Enable EXTI line
//   EXTI_Init(&EXTI_InitStructure);						 
//   
//   /* Initialise the NVIC interrupts (IRQ) using its init structure */
//   NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;        // set IRQ channel to RTC Wakeup Interrupt  
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;	 // set channel Preemption priority to 0
//   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;         // set channel sub priority to 0
//   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;	         // Enable channel
//   NVIC_Init(&NVIC_InitStructure);
//   
//   /* Clear Wake-up flag */  
//   PWR->CR |= PWR_CR_CWUF;

//   /* Enable PWR clock */
//   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 

//   /* Allow access to RTC */
//   PWR_RTCAccessCmd(ENABLE);

//   /* Enable Low Speed External clock */
//   RCC_LSEConfig(RCC_LSE_ON); 

//   /* Wait till LSE is ready */
//   while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

//   /* Select LSE clock as RCC Clock source */
//   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

//   /* Enable the RTC Clock */
//   RCC_RTCCLKCmd(ENABLE);

//   /* Wait for RTC APB registers synchronisation */
//   RTC_WaitForSynchro();

//   /* Select clock for RTC wake up*/
//   RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); // aprox. 2kHz
//   
//   /* Set Wakeup auto-reload value */
//   RTC_SetWakeUpCounter(33); 

//   /* Clear RTC Interrupt pending bit */
//   RTC_ClearITPendingBit(RTC_IT_WUT);
//   
//   /* Clear EXTI line20 Interrupt pending bit */
//   EXTI_ClearITPendingBit(EXTI_Line20);

//   /* Enable the Wakeup Interrupt */
//   RTC_ITConfig(RTC_IT_WUT, ENABLE);
}


void setADCDMA_TransferComplete(void)
{
  flag_ADCDMA_TransferComplete = TRUE;
}

void clearADCDMA_TransferComplete(void)
{
  flag_ADCDMA_TransferComplete = FALSE;
}

void acquireData(void)
{
	uint32_t ch_index;

	  /* Enable HSI Clock */
  RCC_HSICmd(ENABLE);
  
  /*!< Wait till HSI is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
  {}

  /* Enable ADC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

	/* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Wait until the ADC1 is ready */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET); 
  
  for (ch_index = 0; ch_index < MAX_CHNL; ch_index++){
	
		/* Start ADC1 Software Conversion */
		ADC_SoftwareStartConv(ADC1);

		/* Wait until ADC end of conversion */
		while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
		{
		}

		ADC_ConvertedValueBuff[ch_index] = ADC_GetConversionValue(ADC1);
	}
	
}


void acquireDataDMA(void)
{
  /* Enable HSI Clock */
  RCC_HSICmd(ENABLE);
  
  /*!< Wait till HSI is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
  {}
		
		
  /* Enable ADC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  /* Enable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Wait until the ADC1 is ready */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET); 

  /* re-initialize DMA */
  DMA_DeInit(DMA1_Channel1);
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  /* Enable DMA channel 1 Transmit complete interrupt*/
  DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);

  /* Disable DMA mode for ADC1 */ 
  ADC_DMACmd(ADC1, DISABLE);

   /* Enable DMA mode for ADC1 */  
  ADC_DMACmd(ADC1, ENABLE);
  
  /* Clear global flag for DMA transfert complete */
  clearADCDMA_TransferComplete(); 
  
  /* Start ADC conversion */
  ADC_SoftwareStartConv(ADC1);
}

void powerDownADC_DMA(void)
{
  /* Disable DMA channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);  
  /* Disable ADC1 */
  ADC_Cmd(ADC1, DISABLE);

  /* Disable ADC1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, DISABLE);  
  /* Disable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, DISABLE);
}

void powerDownADC(void)
{
  
  /* Disable ADC1 */
  ADC_Cmd(ADC1, DISABLE);

  /* Disable ADC1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, DISABLE);  
  
}

void configureADCforDMA(void)
{
  uint32_t ch_index;

  /* Enable ADC clock & SYSCFG */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
    /* Setup ADC common init struct */
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
  ADC_CommonInit(&ADC_CommonInitStructure);
  
  
  /* Initialise the ADC1 by using its init structure */
  ADC_StructInit(&ADC_InitStructure);
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;	          // Set conversion resolution to 12bit
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;	                          // Enable Scan mode (single conversion for each channel of the group)
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;			  // Disable Continuous conversion
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None; // Disable external conversion trigger
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;                  // Set conversion data alignement to right
  ADC_InitStructure.ADC_NbrOfConversion = ADC_CONV_BUFF_SIZE;             // Set conversion data alignement to ADC_CONV_BUFF_SIZE
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 sequence to sample 16 times ADC_Channel_18 - RV3_VOLTAGE */ 

    for (ch_index = 1; ch_index <= MAX_CHNL; ch_index++){
      ADC_RegularChannelConfig(ADC1, ADC_Channel_18, ch_index, 
                               ADC_SampleTime_384Cycles);
    }
}

void configureADC(void)
{

  /* Enable ADC clock & SYSCFG */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
    /* Setup ADC common init struct */
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
  ADC_CommonInit(&ADC_CommonInitStructure);
  
  
  /* Initialise the ADC1 by using its init structure */
  ADC_StructInit(&ADC_InitStructure);
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;	          // Set conversion resolution to 12bit
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;	                          // Enable Scan mode (single conversion for each channel of the group)
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;			  // Disable Continuous conversion
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None; // Disable external conversion trigger
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;                  // Set conversion data alignement to right
  ADC_InitStructure.ADC_NbrOfConversion = 1;             // Set one conversion
  ADC_Init(ADC1, &ADC_InitStructure);

  /* set ADC1 ADC_Channel_18 - RV3_VOLTAGE */ 

   ADC_RegularChannelConfig(ADC1, ADC_Channel_18, 1, 
                               ADC_SampleTime_384Cycles);
}


void configureDMA(void)
{
  /* Declare NVIC init Structure */
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* Enable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  /* De-initialise  DMA */
  DMA_DeInit(DMA1_Channel1);
  
  /* DMA1 channel1 configuration */
  DMA_StructInit(&DMA_InitStructure);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR);	         // Set DMA channel Peripheral base address to ADC Data register
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValueBuff;  // Set DMA channel Memeory base addr to ADC_ConvertedValueBuff address
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;                         // Set DMA channel direction to peripheral to memory
  DMA_InitStructure.DMA_BufferSize = ADC_CONV_BUFF_SIZE;                     // Set DMA channel buffersize to peripheral to ADC_CONV_BUFF_SIZE
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;	         // Disable DMA channel Peripheral address auto increment
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                    // Enable Memeory increment (To be verified ....)
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;// set Peripheral data size to 8bit 
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;	       // set Memeory data size to 8bit 
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                              // Set DMA in normal mode
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;	                       // Set DMA channel priority to High
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                               // Disable memory to memory option 
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);								 // Use Init structure to initialise channel1 (channel linked to ADC)

  /* Enable Transmit Complete Interrup for DMA channel 1 */ 
  DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
  
  /* Setup NVIC for DMA channel 1 interrupt request */
  NVIC_InitStructure.NVIC_IRQChannel =   DMA1_Channel1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}



void processData(void)
{
  /* sort received data in */
  insertionSort(ADC_ConvertedValueBuff, MAX_CHNL);
  
  /* Calculate the Interquartile mean */
  AVG = interquartileMean(ADC_ConvertedValueBuff, MAX_CHNL); 
}


void RTC_Configuration(void)
{
  
/* Allow access to the RTC */
  PWR_RTCAccessCmd(ENABLE);

  /* Reset Backup Domain */
  RCC_RTCResetCmd(ENABLE);
  RCC_RTCResetCmd(DISABLE);

  /* LSE Enable */
  RCC_LSEConfig(RCC_LSE_ON);

  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {}
  
  RCC_RTCCLKCmd(ENABLE);
   
  /* LCD Clock Source Selection */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

}

/**
  * @brief  To initialize the I/O ports
  * @caller main
  * @param None
  * @retval None
  */


void conf_analog_all_GPIOS(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIOs clock */ 	
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | 
                        RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD | 
                        RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOH, ENABLE);

  /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  GPIO_Init(GPIOH, &GPIO_InitStructure);

#ifdef  DEBUG_SWD_PIN
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All & (~GPIO_Pin_13) & (~GPIO_Pin_14);
#endif
  
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Disable GPIOs clock */ 	
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | 
                        RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD | 
                        RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOH, DISABLE);
}

void  Init_GPIOs (void)
{

  conf_analog_all_GPIOS();   /* configure all GPIOs as analog input */
  
}  



/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
